package com.itgone.view.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;

import com.itgone.R;
import com.itgone.view.adapter.TabViewPagerAdapter;
import com.itgone.view.fragment.CartFragment;
import com.itgone.view.fragment.HomeFragment;
import com.itgone.view.fragment.MineFragment;
import com.itgone.view.fragment.TabFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    private FragmentManager mFragmentManager;
    private ViewPager mViewPager;
    private TabLayout mTabLayout;

    @Override
    int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mViewPager = findViewById(R.id.viewpager);
        mTabLayout = findViewById(R.id.tab_layout);
    }

    @Override
    protected void initData() {
        final List<TabFragment> fragmentList = new ArrayList<>();
        TabFragment homeFragment = new HomeFragment();
        fragmentList.add(homeFragment);
        setTitle(homeFragment.getTitle());
        TabFragment cartFragment = new CartFragment();
        fragmentList.add(cartFragment);
        TabFragment mineFragment = new MineFragment();
        fragmentList.add(mineFragment);

        mFragmentManager = getSupportFragmentManager();
        TabViewPagerAdapter tabViewPagerAdapter = new TabViewPagerAdapter(mFragmentManager, fragmentList);

        mViewPager.setAdapter(tabViewPagerAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                TabFragment tabFragment = fragmentList.get(position);
                String title = tabFragment.getTitle();
                setTitle(title);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mTabLayout.setupWithViewPager(mViewPager);
    }
}
